rm(list = ls())

## Tidy up the data first
library(survey)
library(foreign)
library(doBy)
library(xtable)
library(ggplot2)

cces <- read.dta("CCES_2008b.dta")
cces[cces == "NA"] <- NA


cces_primaryvote <- read.dta("cces2008_primaryvote.dta")
cces<-merge(cces, cces_primaryvote, by=c("source", "respondent"), all.x=T)
cces$weight<-as.numeric(as.vector(cces$weight))

## Create variable with self-report of which party they voted for in the primary
## 1=democrat, 0=republican
cces$presprimary_party<-NA
cces$presprimary_party[cces$cc325a==1 |cces$cc325a==2 | cces$cc325a==3 |cces$cc325a==4] <-1
cces$presprimary_party[cces$cc325b==1 |cces$cc325b==2 | cces$cc325b==3 |cces$cc325b==4] <-1
cces$presprimary_party[cces$cc325a==5 |cces$cc325a==6 | cces$cc325a==7 |cces$cc325a==8 |cces$cc325a==9 |cces$cc325b==10] <-0
cces$presprimary_party[cces$cc325a==5 |cces$cc325b==6 | cces$cc325b==7 |cces$cc325b==8 |cces$cc325b==9 |cces$cc325b==10] <-0

cces$presgeneral_party<-NA
cces$presgeneral_party[cces$president_post_vote==1] <-"Democrat"
cces$presgeneral_party[cces$president_post_vote==0] <-"Republican"


primary_type_dems <- read.csv("Primary Dates and Types 2008-2014 - Dems.csv",colClasses="character")
primary_type_reps <- read.csv("Primary Dates and Types 2008-2014 - Reps.csv",colClasses="character")

# recode dates as days from January 1
the_dates_dems <- as.Date(strptime(primary_type_dems$X2008.President.Date,format="%m/%d/%y"))
primary_type_dems$dem_days <- as.numeric(the_dates_dems - as.Date("2008-01-01"))

the_dates_reps <- as.Date(strptime(primary_type_reps$X2008.President.Date,format="%m/%d/%y"))
primary_type_reps$rep_days <- as.numeric(the_dates_reps - as.Date("2008-01-01"))

primary_type<-as.data.frame(cbind(primary_type_dems$abb, primary_type_dems$dem_days, primary_type_reps$rep_days, primary_type_dems$X2008.President.Type, primary_type_reps$X2008.President.Type))

colnames(primary_type)<-c("abb", "dem_days", "rep_days","dem_type", "rep_type")
primary_type$abb<-as.vector(primary_type$abb)
primary_type$dem_days<-as.numeric(as.vector(primary_type$dem_days))
primary_type$rep_days<-as.numeric(as.vector(primary_type$rep_days))

# merge the data
cces08 <- cbind(primary_type[match(cces$abb,primary_type$abb),],
              cces)

# normalize ideology estimates
cces08$est <- (cces08$est - mean(cces08$est,na.rm=T))/sd(cces08$est)

# scap4 = retrospective primary choice (september)
# bcap4 = prospective primary choice (baseline)
# different primary dates...
# p = post, j = january, m = march, o=october
# e2008p = validated primaryturnout & registration
cces08$primaryturnout <- NA
# first, use known primary primaryturnout and self reports of *which* primary
cces08$primaryturnout[cces08$presprimary_party=="0" & cces08$voted_primary_validated == "1" ] <- "Republican"
cces08$primaryturnout[cces08$presprimary_party=="1" & cces08$voted_primary_validated == "1" ] <- "Democrat"

# for caucuses, use only self-report
cces08$primaryturnout[cces08$presprimary_party=="0" & cces08$rep_type=="Caucus" & cces08$cc324_2 == "1"] <- "Republican"
cces08$primaryturnout[cces08$presprimary_party=="1" & cces08$dem_type=="Caucus" & cces08$cc324_2 == "1"] <- "Democrat"

cces08$pid7<-as.numeric(as.vector(cces08$pid7))

cces08$voted_primary_validated[is.na(cces08$voted_primary_validated) & !is.na(cces08$president_post_vote)]<-0

cces08$general_noprimary[cces08$voted_general_validated==1 & cces08$voted_primary_validated==0]<-1
cces08$general_noprimary[is.na(cces08$general_noprimary) & !is.na(cces08$president_post_vote)]<-0

cces08$gender<-as.numeric(as.vector(cces08$gender))
cces08$idealpoint<-as.numeric(as.vector(cces08$est))
sd(cces08$idealpoint, na.rm=T)
cces08$self_ideology<-as.numeric(as.vector(cces08$self_ideology))
cces08$donor<-as.numeric(as.vector(cces08$donor))
cces08$political_interest2<- as.numeric(as.vector(cces08$political_interest2))
cces08$hs[cces08$education==2]<-1
cces08$hs[cces08$education!=2]<-0

cces08$pol_interest[cces08$political_interest2==1]<-1
cces08$pol_interest[cces08$political_interest2!=1]<-0

cces08$college[cces08$education>=4]<-1
cces08$college[cces08$education<4]<-0

cces08$white[cces08$race==1]<-1
cces08$white[cces08$race!=1]<-0

cces08<-subset(cces08, !is.na(weight) )

survey08<-cces08

survey08$voted_general_validated[is.na(survey08$voted_general_validated)]<-0
survey08$voted_primary_validated[is.na(survey08$voted_primary_validated)]<-0




### Calculate the weighted turnout in general
prop.table(svytable(~voted_general_validated, design=svydesign(~1, weights=~1, data=survey08)))
prop.table(svytable(~voted_general_validated, design=svydesign(~1, weights=~weight, data=survey08)))
### Calculate the weighted turnout in primary
prop.table(svytable(~voted_primary_validated, design=svydesign(~1, weights=~1, data=survey08)))
prop.table(svytable(~voted_primary_validated, design=svydesign(~1, weights=~weight, data=survey08)))

survey08$weight<-as.numeric(as.vector(survey08$weight))
survey08.design<-svydesign(ids=survey08$respondent, data = survey08, weights=survey08$weight)

survey08_primary<-subset(survey08,primaryturnout!="0" )
survey08_primary.design<-svydesign(ids=survey08_primary$respondent, data = survey08_primary, weights=survey08_primary$weight)

survey08_general<-subset( survey08,voted_general_validated=="1" )
survey08_general.design<-svydesign(ids=survey08_general$respondent, data = survey08_general, weights=survey08_general$weight)


survey08_general_noprim<-subset(survey08,( primaryturnout=="0") | (voted_general_validated==1 & voted_primary_validated!=1))
survey08_general_noprim.design<-svydesign(ids=survey08_general_noprim$respondent, data = survey08_general_noprim, weights=survey08_general_noprim$weight)



## make density graph of ideal points
mean_idealpoint_primary_dems <- svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)

mean_idealpoint_partyfollowing_dems <- svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

mean_idealpoint_primary_reps <- svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)

mean_idealpoint_partyfollowing_reps <- svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)
    
survey08$Party<-survey08$pid3
survey08$Party[survey08$pid3==2]<-NA


pdf('density_cces08_cong.pdf', height = 6, width = 7)
ggplot(subset(survey08, !is.na(Party)), aes(idealpoint, fill = Party)) +
geom_density(alpha = 0.1)+ scale_fill_manual(breaks = c("1", "3"),
                        values=c("blue",  "red"),labels=c("Democrats", "Republicans"))+
    geom_vline(xintercept = mean_idealpoint_primary_dems[1], linetype="longdash")+
     geom_vline(xintercept = mean_idealpoint_partyfollowing_dems[1])+
    geom_vline(xintercept = mean_idealpoint_primary_reps[1], linetype="longdash")+
    geom_vline(xintercept = mean_idealpoint_partyfollowing_reps[1])+
      xlab("Ideal Point") +
        theme_bw() 
dev.off()
     
## cross-tabs of who voted
# by party

survey08$voted_primary_validated<-as.numeric(as.vector(survey08$voted_primary_validated))
survey08$general_noprimary<-as.numeric(as.vector(survey08$general_noprimary))
survey08$voted_primary_validated<-as.numeric(as.vector(survey08$voted_primary_validated))
survey08$general_noprimary<-as.numeric(as.vector(survey08$general_noprimary))


svymean(survey08$voted_primary_validated[survey08$pid7<4], design=survey08.design[survey08$pid7<4], na.rm=T)
svymean(survey08$general_noprimary[survey08$pid7<4], design=survey08.design[survey08$pid7<4], na.rm=T)

svymean(survey08$voted_primary_validated[survey08$pid7>4], design=survey08.design[survey08$pid7>4], na.rm=T)
svymean(survey08$general_noprimary[survey08$pid7>4], design=survey08.design[survey08$pid7>4], na.rm=T)


fulltable <- NULL

## age
col1<-svyquantile(~survey08_primary$age_exact[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T, quantile=.5)
col2<-svyquantile(~survey08_general_noprim$age_exact[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T, quantile=.5)
col3<-svyquantile(~survey08_general$age_exact[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T, quantile=.5)

col4<-svyquantile(~survey08_primary$age_exact[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T, quantile=.5)
col5<-svyquantile(~survey08_general_noprim$age_exact[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T, quantile=.5)
col6<-svyquantile(~survey08_general$age_exact[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T, quantile=.5)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "age"; fulltable <- rbind(fulltable,mat)




## hs degree
col1<-svymean(survey08_primary$hs[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$hs[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$hs[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$hs[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$hs[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$hs[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "hs degree"; fulltable <- rbind(fulltable,mat)

## college degree
col1<-svymean(survey08_primary$college[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$college[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$college[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$college[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$college[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$college[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "college"; fulltable <- rbind(fulltable,mat)

## white
col1<-svymean(survey08_primary$white[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$white[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$white[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$white[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$white[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$white[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)
diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "white"; fulltable <- rbind(fulltable,mat)

## very interested in politics
col1<-svymean(survey08_primary$pol_interest[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$pol_interest[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$pol_interest[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$pol_interest[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$pol_interest[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$pol_interest[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "very interested"; fulltable <- rbind(fulltable,mat)


## ideological self-placement
col1<-svymean(survey08_primary$self_ideology[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$self_ideology[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$self_ideology[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$self_ideology[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$self_ideology[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$self_ideology[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "self-placement"; fulltable <- rbind(fulltable,mat)

## Issue ideology
##CCES
col1<-svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Democrat" & survey08_primary$source=="CCES_2008"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat" & survey08_primary$source=="CCES_2008"], na.rm=T)
col2<-svymean(survey08_general_noprim$idealpoint[survey08_general_noprim$presgeneral_party=="Democrat" & survey08_general_noprim$source=="CCES_2008"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat" & survey08_general_noprim$source=="CCES_2008"], na.rm=T)
col3<-svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4 & survey08_general$source=="CCES_2008"], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4 & survey08_general$source=="CCES_2008"], na.rm=T)

col4<-svymean(survey08_primary$idealpoint[survey08_primary$primaryturnout=="Republican" & survey08_primary$source=="CCES_2008"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican" & survey08_primary$source=="CCES_2008"], na.rm=T)
col5<-svymean(survey08_general_noprim$idealpoint[survey08_general_noprim$presgeneral_party=="Republican" & survey08_general_noprim$source=="CCES_2008"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican" & survey08_general_noprim$source=="CCES_2008"], na.rm=T)
col6<-svymean(survey08_general$idealpoint[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4 & survey08_general$source=="CCES_2008"], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4 & survey08_general$source=="CCES_2008"], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "ideology"; fulltable <- rbind(fulltable,mat)

## primary self report

col7<-svymean(survey08$idealpoint[(survey08$presgeneral_party=="Democrat" | survey08$pid7<4) & survey08$voted_primary_selfreport==1], design=survey08.design[(survey08$presgeneral_party=="Democrat" | survey08$pid7<4) & survey08$voted_primary_selfreport==1], na.rm=T)

col8<-svymean(survey08$idealpoint[(survey08$presgeneral_party=="Republican" | survey08$pid7>4) & survey08$voted_primary_selfreport==1], design=survey08.design[(survey08$presgeneral_party=="Republican" | survey08$pid7>4) & survey08$voted_primary_selfreport==1], na.rm=T)

xtable(as.matrix(cbind(col1, col7, NA, col4,  col8)))



## Raise Minimum Wage
col1<-svymean(survey08_primary$cces2006_minimumwage[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$cces2006_minimumwage[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$cces2006_minimumwage[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$cces2006_minimumwage[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$cces2006_minimumwage[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$cces2006_minimumwage[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "raise minimum wage"; fulltable <- rbind(fulltable,mat)


## primary self report

col7<-svymean(survey08$cces2006_minimumwage[(survey08$presgeneral_party=="Democrat" | survey08$pid7<4) & survey08$voted_primary_selfreport==1], design=survey08.design[(survey08$presgeneral_party=="Democrat" | survey08$pid7<4) & survey08$voted_primary_selfreport==1], na.rm=T)

col8<-svymean(survey08$cces2006_minimumwage[(survey08$presgeneral_party=="Republican" | survey08$pid7>4) & survey08$voted_primary_selfreport==1], design=survey08.design[(survey08$presgeneral_party=="Republican" | survey08$pid7>4) & survey08$voted_primary_selfreport==1], na.rm=T)
col7<-paste(round(col7*100,0), "%",sep="")
col8<-paste(round(col8*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col7, NA, col4,  col8)))

#

## Abortion legal -- validated voters
col1<-svymean(survey08_primary$cces2006_abortion[survey08_primary$primaryturnout=="Democrat"], design=survey08_primary.design[survey08_primary$primaryturnout=="Democrat"], na.rm=T)
col2<-svymean(survey08_general_noprim$cces2006_abortion[survey08_general_noprim$presgeneral_party=="Democrat"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Democrat"], na.rm=T)
col3<-svymean(survey08_general$cces2006_abortion[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], design=survey08_general.design[survey08_general$presgeneral_party=="Democrat" | survey08_general$pid7<4], na.rm=T)

col4<-svymean(survey08_primary$cces2006_abortion[survey08_primary$primaryturnout=="Republican"], design=survey08_primary.design[survey08_primary$primaryturnout=="Republican"], na.rm=T)
col5<-svymean(survey08_general_noprim$cces2006_abortion[survey08_general_noprim$presgeneral_party=="Republican"], design=survey08_general_noprim.design[survey08_general_noprim$presgeneral_party=="Republican"], na.rm=T)
col6<-svymean(survey08_general$cces2006_abortion[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], design=survey08_general.design[survey08_general$presgeneral_party=="Republican" | survey08_general$pid7>4], na.rm=T)

diff_a<-col1-col3
diff_b<-col4-col6
z_test_a = (col1-col3)/sqrt(as.data.frame(col1)$SE^2 + as.data.frame(col3)$SE^2)
z_test_b= (col4-col6)/sqrt(as.data.frame(col4)$SE^2 + as.data.frame(col6)$SE^2)

col1<-paste(round(col1*100,0), "%",sep="")
col2<-paste(round(col2*100,0), "%",sep="")
col3<-paste(round(col3*100,0), "%",sep="")
col4<-paste(round(col4*100,0), "%",sep="")
col5<-paste(round(col5*100,0), "%",sep="")
col6<-paste(round(col6*100,0), "%",sep="")
diff_a<-paste(round(diff_a*100,0), "%",sep="")
diff_b<-paste(round(diff_b*100,0), "%",sep="")

#xtable(as.matrix(cbind(col1, col3, diff_a,NA, col4, col6, diff_b)))
xtable(as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6))); mat <- as.matrix(cbind(col1, col2, col3,NA, col4, col5, col6)); rownames(mat) <- "abortion"; fulltable <- rbind(fulltable,mat)

## primary self report

col7<-svymean(survey08$cces2006_abortion[(survey08$presgeneral_party=="Democrat" | survey08$pid7<4) & survey08$voted_primary_selfreport==1], design=survey08.design[(survey08$presgeneral_party=="Democrat" | survey08$pid7<4) & survey08$voted_primary_selfreport==1], na.rm=T)

col8<-svymean(survey08$cces2006_abortion[(survey08$presgeneral_party=="Republican" | survey08$pid7>4) & survey08$voted_primary_selfreport==1], design=survey08.design[(survey08$presgeneral_party=="Republican" | survey08$pid7>4) & survey08$voted_primary_selfreport==1], na.rm=T)
col7<-paste(round(col7*100,0), "%",sep="")
col8<-paste(round(col8*100,0), "%",sep="")

xtable(as.matrix(cbind(col1, col7, NA, col4,  col8)))


fulltable
xtab <- print(xtable(fulltable))
cat(xtab,file="cces2008.tex")

